/**
 * Created by Administrator on 2016/8/6.
 * 用来响应用于注册
 */
var express = require('express');
var router = express.Router();
var User = require('../db/user');
var result = require('../utils/result');
router.get('/', function (req, res, next) {
    res.render('register');
});
router.post('/', function (req, res, next) {
    var r = result();
    var username = req.body.username;
    var p = req.body.password;
/*    console.log(Md5(d));*/
    var u = new User({
        name: username,
        pwd: p,
        register: Date.now()
    });
    User.find({name:username}, function (err, docs) {
        if(docs==""){
            u.save(function (err, doc) {
                if (err) {
                    r.error = err;
                } else {
                    r.data = 'ok';
                }
                return res.json(r);
            });
        }else {
            r.data = 'no';
            return res.json(r);
        }
    });

});

module.exports = router;
